package com.ruoyi.quanzi.controller.app;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunTopic;
import com.ruoyi.quanzi.service.ISunTopicService;

/**
 * 话题管理Controller
 * 
 * @author ruoyi
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/app/topic")
public class AppTopicController extends BaseController
{
    @Autowired
    private ISunTopicService sunTopicService;

    /**
     * 查询话题列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SunTopic sunTopic)
    {
        startPage();
        List<SunTopic> list = sunTopicService.selectSunTopicList(sunTopic);
        return getDataTable(list);
    }

    /**
     * 获取话题详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunTopicService.selectSunTopicById(id));
    }

    /**
     * 新增话题
     */
    @PostMapping
    public AjaxResult add(@RequestBody SunTopic sunTopic)
    {
        // 设置当前登录用户ID
        sunTopic.setCreateBy(getUsername());
        
        return toAjax(sunTopicService.insertSunTopic(sunTopic));
    }

    /**
     * 修改话题
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SunTopic sunTopic)
    {
        // 设置当前登录用户ID
        sunTopic.setUpdateBy(getUsername());
        
        return toAjax(sunTopicService.updateSunTopic(sunTopic));
    }

    /**
     * 删除话题
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunTopicService.deleteSunTopicByIds(ids));
    }
    
    /**
     * 获取热门话题
     */
    @GetMapping("/hot")
    public AjaxResult getHotTopics()
    {
        List<SunTopic> hotTopics = sunTopicService.selectHotTopics();
        return success(hotTopics);
    }
    
    /**
     * 获取推荐话题
     */
    @GetMapping("/recommend")
    public AjaxResult getRecommendTopics()
    {
        List<SunTopic> recommendTopics = sunTopicService.selectRecommendTopics();
        return success(recommendTopics);
    }
    
    /**
     * 关注话题
     */
    @PostMapping("/follow/{topicId}")
    public AjaxResult followTopic(@PathVariable("topicId") Long topicId)
    {
        return toAjax(sunTopicService.followTopic(topicId, getUserId()));
    }
    
    /**
     * 取消关注话题
     */
    @DeleteMapping("/unfollow/{topicId}")
    public AjaxResult unfollowTopic(@PathVariable("topicId") Long topicId)
    {
        return toAjax(sunTopicService.unfollowTopic(topicId, getUserId()));
    }
    
    /**
     * 获取用户关注的话题
     */
    @GetMapping("/followed")
    public TableDataInfo getFollowedTopics()
    {
        startPage();
        List<SunTopic> followedTopics = sunTopicService.selectFollowedTopics(getUserId());
        return getDataTable(followedTopics);
    }
}